import fetch from 'node-fetch';

async function testAllAdminApis() {
    console.log('开始完整测试管理后台API接口...\n');
    
    const baseUrl = 'http://localhost:8000/api/v1/admin';
    const authToken = 'Bearer admin_token';
    
    try {
        // 测试接口15: 获取统计数据
        console.log('🔥 测试接口15: 获取统计数据');
        const response1 = await fetch(`${baseUrl}/data/info`, {
            method: 'GET',
            headers: {
                'Authorization': authToken
            }
        });
        const result1 = await response1.text();
        console.log(`状态码: ${response1.status}`);
        console.log(`响应: ${result1}\n`);
        
        // 测试接口16: 日新增文章数量统计
        console.log('🔥 测试接口16: 日新增文章数量统计');
        const response2 = await fetch(`${baseUrl}/data/article`, {
            method: 'GET',
            headers: {
                'Authorization': authToken
            }
        });
        const result2 = await response2.text();
        console.log(`状态码: ${response2.status}`);
        console.log(`响应: ${result2}\n`);
        
        // 测试接口11: 发布文章
        console.log('🔥 测试接口11: 发布文章');
        const response3 = await fetch(`${baseUrl}/article/publish`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': authToken
            },
            body: JSON.stringify({
                title: '管理后台测试文章',
                content: '这是通过管理后台发布的测试文章内容，用于验证完整的CRUD功能。',
                categoryId: 1,
                date: '2025-08-15',
                state: '已发布'
            })
        });
        const result3 = await response3.text();
        console.log(`状态码: ${response3.status}`);
        console.log(`响应: ${result3}\n`);
        
        // 测试接口13: 编辑文章 (编辑刚才创建的文章，假设ID为224)
        console.log('🔥 测试接口13: 编辑文章');
        const response4 = await fetch(`${baseUrl}/article/edit`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': authToken
            },
            body: JSON.stringify({
                id: 224,
                title: '编辑后的测试文章标题',
                content: '这是编辑后的文章内容，展示了文章更新功能。',
                categoryId: 2,
                date: '2025-08-15',
                state: '已发布'
            })
        });
        const result4 = await response4.text();
        console.log(`状态码: ${response4.status}`);
        console.log(`响应: ${result4}\n`);
        
        // 测试接口14: 删除文章
        console.log('🔥 测试接口14: 删除文章（软删除）');
        const response5 = await fetch(`${baseUrl}/article/delete`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': authToken
            },
            body: JSON.stringify({
                id: 224
            })
        });
        const result5 = await response5.text();
        console.log(`状态码: ${response5.status}`);
        console.log(`响应: ${result5}\n`);
        
        console.log('✅ 所有接口测试完成！');
        
    } catch (error) {
        console.error('测试失败:', error.message);
    }
}

testAllAdminApis();
